"""
This file is part of GSBEdit. For copyright and licensing information, please
see gsbedit.py in the GSBEdit base directory.
"""


import os.path
import os
import copy
import ConfigParser

import game_data.module


dlcs = ['tribe', 'nomads', 'swarm', 'order', 'fedelite']


class Mod:
    def __init__(self, name, package, base_dir):
        self.name = name
        self.base = base_dir
        self.package = package
        
        if self.name == '':
            self.type = 'Core'
        elif self.name in dlcs:
            self.type = 'DLC'
        else:
            self.type = 'Mod'

        self.load_data()
    
    
    def has_package(self):
        return self.package != None


    def get_name(self):
        return self.name
    
    
    def get_friendly_name(self):
        if self.package != None:
            return self.package.get_name()
        else:
            return self.get_name()
    
    
    def get_type(self):
        return self.type
        
        
    def load_data(self):
        self.hulls_dir = self.get_path(['hulls'])
        self.modules_dir = self.get_path(['modules'])

        self.hull_file_list = self.generate_hull_file_list()
        self.reload_module_list()
    
    
    def reload_module_list(self):
        self.module_list = self.generate_module_list()
    
    
    def get_path(self, path):
        return os.path.join(self.base, *path)
    
    
    def generate_module_list(self):
        if not(os.path.exists(self.modules_dir)):
            return {}

        module_list = {}
        for filename in os.listdir(self.modules_dir):
            full_filename = os.path.join(self.modules_dir, filename)
            if not(os.path.isfile(full_filename)):
                print "Skipping '%s'" % filename
                continue
                
            try:
                module = game_data.module.Module(full_filename)
            except:
                print "File '%s' does not appear to be a valid module" % filename
                continue
                
            if module.has_value('config', 'restricted'):
                race = module.get_value('config', 'restricted').title()
            else:
                race = 'Generic'
                
            if not(race in module_list): module_list[race] = {}
            module_name = module.get_value('config', 'name')
            module_list[race][module_name] = module
        return module_list
    
    
    def generate_hull_file_list(self):
        if not(os.path.exists(self.hulls_dir)):
            return {}

        file_list = {}
        for race in os.listdir(self.hulls_dir):
            hulls_dir = self.get_path(['hulls', race])
            file_list[race] = os.listdir(hulls_dir)
        return file_list
    
    
    def get_hull_file_list(self):
        return copy.copy(self.hull_file_list)
        
    
    def get_module_list(self):
        return copy.copy(self.module_list)
    
    
    def get_module(self, race, module):
        return self.module_list[race][module]
    
